package dao.impl;

import dao.BaseDao;
import dao.MetaverseProductDao;
import entity.MetaverseProduct;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MetaverseProductDaoImpl extends BaseDao implements MetaverseProductDao {
    public int delete(int id) {
        int result=0;
        try {
            conn=getConnection();
            String sql="delete from metaverse_product where id=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            result = pstmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return result;
    }
    @Override
    public List<MetaverseProduct> selectAll() {
        List<MetaverseProduct> list = new ArrayList<>();
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select id,product_name,product_price,product_desc,flag from metaverse_product ");
            rs = pstmt.executeQuery();
            while (rs.next()){
                MetaverseProduct metaverseProduct =qu(rs);
                list.add(metaverseProduct);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    @Override
    public int update(int flag, int id) {
        int result  = -1;
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("UPDATE `metaverse_product` SET `flag` = ? WHERE `id` = ?; ");
            pstmt.setInt(1,flag);
            pstmt.setInt(2,id);
            result = pstmt.executeUpdate();
        }catch (SQLException E){
            E.printStackTrace();
        }finally {
            closeAll(conn,pstmt);
        }
        return result;
    }

    @Override
    public int insert(MetaverseProduct product) {
        int result = -1;
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("INSERT INTO `metaverse_product`( `product_name`, `product_price`, `product_desc`, `flag`) VALUES ( ?, ?, ?, ?)");
            pstmt.setString(1,product.getProductName());
            pstmt.setDouble(2,product.getProductPrice());
            pstmt.setString(3,product.getProductDesc());
            pstmt.setInt(4,product.getFlag());
            result = pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt);
        }
        return result;
    }

    public MetaverseProduct qu(ResultSet rs){
        MetaverseProduct metaverseProduct = new MetaverseProduct();
        try {
            metaverseProduct.setId( rs.getInt(1));
            metaverseProduct.setProductName( rs.getString(2));
            metaverseProduct.setProductPrice( rs.getDouble(3));
            metaverseProduct.setProductDesc( rs.getString(4));
            metaverseProduct.setFlag( rs.getInt(5));
        }catch (SQLException e){
            e.printStackTrace();
        }
        return metaverseProduct;
    }
}
